package com.ebeitech.library.ui;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ebeitech.library.R;
import com.ebeitech.library.util.StringUtil;

/**
 * Created by alice on 2017/10/31.
 * 通用的标题栏
 *
 * @author alice
 */
public class CommonTitleBar extends LinearLayout {

	Button btnBack;
	ImageView btnLeft;
	TextView tvTitle;
	ImageView btnRight;
	Button btnRightText;

	private Context mContext;
	private String titleText;
	private String leftText;
	private String rightText;
	private Drawable imgLeft;
	private Drawable imgRight;
	private boolean showLeft;
	private boolean showRight;
	private boolean showLeftTextOnly;
	private boolean showLeftImgOnly;

	public void onBackClick(View view) {
		((Activity) mContext).finish();
	}

	public void setBtnBackVisible(boolean isVisible) {
		btnBack.setVisibility(isVisible ? VISIBLE : INVISIBLE);
	}

	public void setTitle(String titleText) {
		if (!StringUtil.isStringNullOrEmpty(titleText)) {
			tvTitle.setText(titleText);
		}
	}

	public void setBtnLeftImage(int resId) {
		btnBack.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(resId), null, null, null);
	}

	public void setBtnRightImage(int resId) {
		btnRight.setImageResource(resId);
	}

	public void setBtnRightText(String rightText, View.OnClickListener l) {
		btnRightText.setOnClickListener(l);
		if (!StringUtil.isStringNullOrEmpty(rightText)) {
			btnRightText.setText(rightText);
			btnRightText.setVisibility(View.VISIBLE);
			btnRight.setVisibility(View.GONE);
		} else {
			if (imgRight != null) {
				btnRight.setImageDrawable(imgRight);
				btnRight.setVisibility(View.VISIBLE);
			} else {
				btnRight.setVisibility(View.GONE);
			}
			btnRightText.setVisibility(View.GONE);
		}
	}

	public CommonTitleBar(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		mContext = context;
		initView(context);
	}

	public CommonTitleBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		mContext = context;
		setCustomAttributes(attrs);
		initView(context);
	}

	public CommonTitleBar(Context context, AttributeSet attrs, int defStyle) {
		// TODO Auto-generated constructor stub
		super(context, attrs, defStyle);
		mContext = context;
		setCustomAttributes(attrs);
		initView(context);
	}

	private void setCustomAttributes(AttributeSet attrs) {
		// 读取传入参数
		TypedArray array = mContext.obtainStyledAttributes(attrs, R.styleable.EbeiTopbar);
		titleText = array.getString(R.styleable.EbeiTopbar_titleText);
		String leftTextT = array.getString(R.styleable.EbeiTopbar_leftText);
		if (!StringUtil.isStringNullOrEmpty(leftTextT)) {
			leftText = leftTextT;
		} else {
			leftText = mContext.getString(R.string.ebei_back);
		}
		rightText = array.getString(R.styleable.EbeiTopbar_rightText);
		imgLeft = array.getDrawable(R.styleable.EbeiTopbar_leftImg);
		imgRight = array.getDrawable(R.styleable.EbeiTopbar_rightImg);
		showLeft = array.getBoolean(R.styleable.EbeiTopbar_leftVisible, true);
		showRight = array.getBoolean(R.styleable.EbeiTopbar_rightVisible, false);
		showLeftTextOnly = array.getBoolean(R.styleable.EbeiTopbar_leftTextOnly, false);
		showLeftImgOnly = array.getBoolean(R.styleable.EbeiTopbar_leftImgOnly, false);
	}

	private void initView(Context context) {
		// TODO Auto-generated method stub
		View view = LayoutInflater.from(mContext).inflate(R.layout.ebei_view_common_title_bar, null);
		btnBack = view.findViewById(R.id.btnBack);
		btnBack.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				onBackClick(v);
			}
		});
		btnLeft = view.findViewById(R.id.btnLeft);
		tvTitle = view.findViewById(R.id.tvTitle);
	    btnRight = view.findViewById(R.id.btnRight);
		btnRightText = view.findViewById(R.id.btnRightText);

		/*btnBack.setOnLongClickListener(new View.OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				// TODO Auto-generated method stub
				Activity mActivity = (Activity) mContext;
				Intent logoutApp = new Intent(QPIConstants.ACTION_EXIT_APP);
				logoutApp.putExtra("isLogout", false);
				mActivity.sendBroadcast(logoutApp);

				Intent intent = new Intent(mContext, HomePageLandActivity.class);
				mActivity.startActivity(intent);
				mActivity.finish();
				mActivity.overridePendingTransition(R.anim.ebei_in_from_left, R.anim.ebei_out_to_right);
				return false;
			}
		});*/

		int height = (int) getResources().getDimension(R.dimen.ebei_title_bar_image_size);
		addView(view, new LayoutParams(LayoutParams.MATCH_PARENT, height));

		if (!StringUtil.isStringNullOrEmpty(titleText)) {
			tvTitle.setText(titleText);
		}
		if (showLeft) {
			/*if (imgLeft != null) {
				leftImage.setImageDrawable(imgLeft);
			}*/
			if (!StringUtil.isStringNullOrEmpty(leftText)) {
				btnBack.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.ebei_btn_back), null, null, null);
				btnBack.setText(leftText);
				btnBack.setVisibility(View.VISIBLE);
				btnLeft.setVisibility(View.GONE);
			} else {
				if (imgLeft != null) {
					btnLeft.setImageDrawable(imgLeft);
					btnLeft.setVisibility(View.VISIBLE);
				} else {
					btnLeft.setVisibility(View.GONE);
				}
				btnBack.setVisibility(View.GONE);
			}
			if (showLeftImgOnly) {
				btnBack.setVisibility(View.GONE);
				btnLeft.setVisibility(View.VISIBLE);
			} else if (showLeftTextOnly) {
				btnBack.setVisibility(View.VISIBLE);
				btnLeft.setVisibility(View.GONE);
			}
		} else {
			btnBack.setVisibility(View.INVISIBLE);
			btnLeft.setVisibility(View.GONE);
		}
		if (showRight) {
			if (!StringUtil.isStringNullOrEmpty(rightText)) {
				btnRightText.setText(rightText);
				btnRightText.setVisibility(View.VISIBLE);
				btnRight.setVisibility(View.GONE);
			} else {
				if (imgRight != null) {
					btnRight.setImageDrawable(imgRight);
					btnRight.setVisibility(View.VISIBLE);
				} else {
					btnRight.setVisibility(View.GONE);
				}
				btnRightText.setVisibility(View.GONE);
			}
			btnRight.setVisibility(View.VISIBLE);
		} else {
			btnRight.setVisibility(View.GONE);
			btnRightText.setVisibility(View.GONE);
		}
	}

}
